import pandas as pd
import re
from collections import Counter
import hashlib

class Word_Count():

    def __init__(self):
        self.filename = './word2count.txt'
        self.punc = '~`!#$%^&*()_+-=|\';":/.,?><~·！@#￥%……&*（）——+-=“：’；、。，？》《{}\n\r'
        self.pa_list = []
        self.sum = 0

    def to_csv(self,word_count):
        for key,value in word_count.items():
            self.pa_list.append([key,value,hashlib.md5(key.encode('utf-8')).hexdigest()])
        pd_data = pd.DataFrame(self.pa_list,columns=['单词','词频','MD5值'])
        pd_data.drop(labels=[13],inplace=True)
        dataexclex = pd_data.sort_values(['词频','MD5值'])
        for i in dataexclex['词频']:
            self.sum = self.sum + i
        print('共计' + str(len(dataexclex)) + '个单词\n'+'全文单词数为'+str(self.sum))
        dataexclex.to_csv('./20020308_word_result.csv',index=False,encoding='utf_8_sig')

    def count(self,txt_list):
        word_count = Counter(txt_list)
        self.to_csv(word_count)

    def Remove_the_special_characters(self,txt_data):
        txt_lower = txt_data.lower()
        txt_query = re.sub(r"[%s]+" %self.punc, " ",txt_lower)
        txt_list = txt_query.split(' ')
        self.count(txt_list)
    def open_file(self):
        txt_file = open('./word2count.txt','r')
        txt_data = txt_file.read()
        txt_file.close()
        self.Remove_the_special_characters(txt_data)

if __name__=='__main__':
    Word_Count = Word_Count()
    Word_Count.open_file()




